﻿using UnityEngine;
using System.Collections;
using LiftIndicatorMOD;
using System.Collections.Generic;

public class LiftBlockScript : MonoBehaviour
{

    /// <summary>
    /// 场景中存在的升力模块列表
    /// </summary>
    public static List<LiftBlockScript> liftBlocks = new List<LiftBlockScript>();

    /// <summary>
    /// 升力向量
    /// </summary>
    public Vector3 liftVector { get; private set; } = Vector3.zero;

    /// <summary>
    /// 升力可视
    /// </summary>
    public bool Visiable = true;

    /// <summary>
    /// 参与合力计算
    /// </summary>
    public bool Calculate = true;

    LineRenderer lr;

    AxialDrag ad;

    ConfigurableJoint cj;

    // Use this for initialization
    void Start()
    {

        liftBlocks.Add(this);
        ad = GetComponent<AxialDrag>();
        cj = GetComponent<ConfigurableJoint>();
        if (GetComponent<LineRenderer>() == null)
        {
            lr = gameObject.AddComponent<LineRenderer>();
        }
        else
        {
            lr = GetComponent<LineRenderer>();
        }
            lr.useWorldSpace = true;
            lr.SetVertexCount(2);
            lr.material = new Material(Shader.Find("Particles/Additive"));
            lr.SetColors(Color.red, Color.yellow);
            lr.SetWidth(0.5f, 0.5f);
        
    }

    // Update is called once per frame
    void Update()
    {

        lr.enabled = Visiable && SettingWindow.liftVisiabled;

        if (!StatMaster.isSimulating)
        {
            //模拟速度向量转换到升力模块的坐标
            ad.xyz = Vector3.Scale(ad.rigid.transform.InverseTransformDirection(SettingWindow.simulateVelocity_Vector), ad.AxisDrag);
            //计算模拟速度向量模的平方
            ad.currentVelocitySqr = Mathf.Min(SettingWindow.simulateVelocity_Vector.sqrMagnitude, GetComponent<BlockBehaviour>().GetBlockID() == (int)BlockType.Wing ? 100 : 900);         
        }

        liftVector = ad.rigid.transform.TransformVector(ad.xyz * ad.currentVelocitySqr);

        if (lr.enabled)
        {
            lr.SetPosition(0, transform.position);
            lr.SetPosition(1, transform.position + liftVector);
        }
        if (cj == null)
        {
            SetCalculate(false);
            SetVisiable(false);
        }
    }
    

    private void OnDestroy()
    {
        if (!StatMaster.isSimulating)
        {
            liftBlocks.Remove(this);
        }     
    }

    public void SetVisiable(bool value)
    {
        Visiable = lr.enabled = value;
    }

    public void SetCalculate(bool value)
    {
        Calculate = value;
    }



    /// <summary>
    /// 通过零件ID判断是否是升力模块
    /// </summary>
    /// <param name="id">零件ID</param>
    /// <returns></returns>
    public static bool isLiftBlock(int id)
    {
        return (id == (int)BlockType.Propeller) || (id == (int)BlockType.SmallPropeller) || (id == (int)BlockType.Wing) || (id == (int)BlockType.WingPanel) || (id == 52);
    }

    public static void SetAllVisiable(bool value)
    {
        foreach (var block in liftBlocks)
        {
            block.GetComponent<LiftBlockScript>().SetVisiable(value);
        }
    }

    public static void SetAllEnable(bool value)
    {

        foreach (var block in liftBlocks)
        {
            block.GetComponent<LiftBlockScript>().SetVisiable(value);
            block.GetComponent<LiftBlockScript>().enabled = value;
        }
    }

}
